/*
 * Created by JFormDesigner on Wed Jul 07 13:03:50 CST 2021
 */

package user;

import javax.swing.event.*;
import com.sun.javaws.util.JfxHelper;
import controller.ReceiveFile;
import model.FileModel;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.*;
import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.swing.*;
import javax.swing.filechooser.FileSystemView;

/**
 * @author DarkFire
 */
public class Receive extends JPanel {
    private static JButton btnReceiveFile;
    private static JLabel textReceiveProcess;

    public Receive() {
        initComponents();
    }

    //选择文件保存目录
    private void btnSaveFileActionPerformed(ActionEvent e) {
       JFileChooser jFileChooser=new JFileChooser();
       jFileChooser.setDialogTitle("请选择保存路径");
       int returnVal = jFileChooser.showOpenDialog(null);
       if (returnVal==JFileChooser.APPROVE_OPTION){
           File file= jFileChooser.getCurrentDirectory() ;
           textPath.setText(file.getPath());

       }

    }


    //单击标签事件
    private void textIPMouseClicked(MouseEvent e) {
        // 获取系统剪贴板
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        // 封装文本内容
        Transferable trans = new StringSelection(textIP.getText());
        // 把文本内容设置到系统剪贴板
        clipboard.setContents(trans, null);
        JOptionPane.showMessageDialog(null,"已复制IP地址");

    }

    private void textIPAncestorAdded(AncestorEvent e) {
        // TODO add your code here
    }

    private void btnReceiveFileActionPerformed(ActionEvent e) {
        File dir=new File(textPath.getText());
        if (!dir.exists()) {// 判断目录是否存在
            dir.mkdir();
        }else {
            FileModel fileModel = new FileModel(dir);
            btnReceiveFile.setEnabled(false);
            new ReceiveFile(fileModel);
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        textIP = new JLabel();
        label2 = new JLabel();
        textPath = new JTextField();
        btnSaveFile = new JButton();
        label3 = new JLabel();
        btnReceiveFile = new JButton();
        textReceiveProcess = new JLabel();

        //======== this ========
        setLayout(null);

        //---- label1 ----
        label1.setText("\u4f60\u7684IP:");
        add(label1);
        label1.setBounds(15, 15, 45, label1.getPreferredSize().height);

        //---- textIP ----
        textIP.setText("127.0.0.1");
        textIP.setForeground(new Color(255, 51, 51));
        textIP.addAncestorListener(new AncestorListener() {
            @Override
            public void ancestorAdded(AncestorEvent e) {
                textIPAncestorAdded(e);
            }
            @Override
            public void ancestorMoved(AncestorEvent e) {}
            @Override
            public void ancestorRemoved(AncestorEvent e) {}
        });
        textIP.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                textIPMouseClicked(e);
            }
        });
        add(textIP);
        textIP.setBounds(65, 15, 95, textIP.getPreferredSize().height);

        //---- label2 ----
        label2.setText("\u6587\u4ef6\u63a5\u6536\u8def\u5f84:");
        add(label2);
        label2.setBounds(15, 40, 105, 17);

        //---- textPath ----
        textPath.setText("C:\\Users\\user\\Desktop");
        textPath.setEnabled(false);
        add(textPath);
        textPath.setBounds(15, 60, 335, 25);

        //---- btnSaveFile ----
        btnSaveFile.setText("\u9009\u62e9\u4fdd\u5b58\u8def\u5f84");
        btnSaveFile.addActionListener(e -> btnSaveFileActionPerformed(e));
        add(btnSaveFile);
        btnSaveFile.setBounds(200, 30, 140, btnSaveFile.getPreferredSize().height);

        //---- label3 ----
        label3.setText("\u7aef\u53e3:9999");
        add(label3);
        label3.setBounds(255, 15, 90, 17);

        //---- btnReceiveFile ----
        btnReceiveFile.setText("\u5f00\u59cb\u63a5\u6536\u6587\u4ef6");
        btnReceiveFile.addActionListener(e -> btnReceiveFileActionPerformed(e));
        add(btnReceiveFile);
        btnReceiveFile.setBounds(20, 100, 330, btnReceiveFile.getPreferredSize().height);

        //---- textReceiveProcess ----
        textReceiveProcess.setText(" ");
        textReceiveProcess.setForeground(Color.red);
        textReceiveProcess.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 14));
        add(textReceiveProcess);
        textReceiveProcess.setBounds(25, 145, 355, 35);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < getComponentCount(); i++) {
                Rectangle bounds = getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            setMinimumSize(preferredSize);
            setPreferredSize(preferredSize);
        }
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
        try {
            textIP.setText(InetAddress.getLocalHost().getHostAddress());
            File desktopDir = FileSystemView.getFileSystemView().getHomeDirectory();
            String desktopPath = desktopDir.getAbsolutePath();
            textPath.setText(desktopPath+"/DarkFireFile/");
        } catch (UnknownHostException unknownHostException) {
            unknownHostException.printStackTrace();
        }
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JLabel textIP;
    private JLabel label2;
    private JTextField textPath;
    private JButton btnSaveFile;
    private JLabel label3;

    // JFormDesigner - End of variables declaration  //GEN-END:variables
    public static JButton getbtnReceiveFile(){
        return  btnReceiveFile;
    }
    public  static  JLabel getTextReceiveProcess(){

        return textReceiveProcess;
    }
}
